package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

//合同
@Service
public class ContractServiceImpl implements ContractService {
    @Autowired
    private ContractDao contractDao;

    @Override
    public Contract findById(String id) {
        return contractDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Contract contract) {
        // 手动设置合同的基本参数（uuid 状态  货物数 附件数）
        //设置主键
        contract.setId(UUID.randomUUID().toString().replaceAll("-",""));
        //设置状态 0：草稿  1：以上报  2：以报运
        contract.setState(0);
        //设置货物数
        contract.setProNum(0);
        //设置附件数
        contract.setExtNum(0);
        //设置总金额
        contract.setTotalAmount(0.0);
        //设置合同创建的时间
        contract.setShipTime(new Date());
        //保存初始化的合同
        contractDao.insertSelective(contract);

    }

    @Override
    public void update(Contract contract) {
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        contractDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<Contract> list = contractDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public List<ContractProductVo> findOutDate(String companyId, String inputDate) {
        return contractDao.findOutDate(companyId,inputDate);
    }
}
